/**
 * 
 */
package com.rocky.project.action.report;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionSupport;
import com.rocky.project.model.family.Archive;
import com.rocky.project.model.family.Attachment;
import com.rocky.project.model.family.Family;
import com.rocky.project.model.family.Person;
import com.rocky.project.service.BaseService;
import com.rocky.project.util.WebUtil;

/**
 * @author chen 土地档案生成的报表Action
 */
public class ArchiveReporter extends ActionSupport implements IReporter {

	protected Logger log = Logger.getLogger(this.getClass());

	private static final long serialVersionUID = 1764043235575919803L;

	private BaseService baseService;

	private Archive archive;

	private ArchiveVO vo = new ArchiveVO();

	private List<ArchiveVO> voList = new ArrayList<ArchiveVO>(0);
	
	private String suc;
	
	

	public String getSuc() {
		return suc;
	}

	public void setSuc(String suc) {
		this.suc = suc;
	}

	/**
	 * 模拟测试用
	 */
	@SuppressWarnings("unused")
	private void copyArchiveToVo() {
		// vo=new ArchiveVO();
		this.vo.setAutoNum(this.archive.getAutonum());
		this.vo.setAreaCard(this.archive.getAreacard());
		this.vo.setAreaNum(this.archive.getAreanum());
		this.vo.setFoundationArea(this.archive.getFoundationArea());
		this.vo.setConstructionArea(this.archive.getConstructionArea());
		this.vo.setUseful(this.archive.getUseful());
		this.vo.setOwnType(this.archive.getOwntype());

		Family fm = this.archive.getFamily();
		if (fm != null) {
			this.vo.setHouseHolder(fm.getHouseholdername());
			Person per = fm.getHouseholder();
			this.vo.setPostCode(fm.getFamilycode());
			this.vo.setAddress(fm.getAddress());
			if (per != null) {
				this.vo.setIdCard(per.getCardnumber());
				this.vo.setSex(per.getSex());
				Date date = per.getBirthday();
				if (date != null)
					this.vo.setBirthday((new SimpleDateFormat("yyyy-MM-dd")).format(date));

			}
			Set set = fm.getPersons();
			if (set != null) {
				this.vo.setPersonNum(String.valueOf(set.size()));
			} else {
				this.vo.setPersonNum("0");
			}
		}
	}

	/**
	 * 模拟测试用
	 */
	@SuppressWarnings({ "unchecked", "deprecation", "unchecked" })
	private void mockArchive() {
		log.debug("开始模拟数据测试！");
		archive = new Archive();
		Family family = new Family();
		family.setHouseholdername("户主姓名");
		family.setAddress("家庭住址");
		family.setFamilycode("邮政编码");
		Person person = new Person();
		person.setCardnumber("350100198202020202");
		person.setName("户主姓名");
		person.setBirthday(new Date(1982, 2, 2));
		person.setSex("2");
		person.setResidenceType("户籍类型");
		family.setHouseholder(person);
		Set persons = new HashSet();
		persons.add(new Object());
		family.setPersons(persons);
		archive.setFamily(family);
		Attachment pho = new Attachment();
		pho.setDisplayName("displayName");
		pho.setType("1");
		pho.setRelativePath("c:\\1.jpg");
		pho.setSize(100L);
		pho.setName("title");
		Set photoes = new HashSet();
		photoes.add(pho);
		archive.setPhotos(photoes);

		Attachment pic = new Attachment();
		pic.setDisplayName("picdisplayName");
		pic.setType("2");
		pic.setRelativePath("c:\\2.jpg");
		pic.setSize(1000L);
		pic.setName("title");
		Set pics = new HashSet();
		pics.add(pic);
		archive.setPics(pics);
		archive.setAreacard("土地证号");
		archive.setAreanum("dihao");
		archive.setAutonum("shenpibiaohao");
		archive.setUseful("usefull");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rocky.project.action.report.IReporter#csv()
	 */
	public String csv() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rocky.project.action.report.IReporter#excel()
	 */
	public String excel() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rocky.project.action.report.IReporter#pdf()
	 */
	public String pdf() throws Exception {
		String id = WebUtil.getParaValue("id");
		if (id != null && !"".equals(id)) {
			this.archive = (Archive) baseService.get(Archive.class, Long.valueOf(id));
			log.debug("获取archive对象成功，准备初始化数据给jasperreport！");
		} else {
			this.mockArchive();
		}
		this.vo.initArchive(this.archive);
		this.vo.setSubreport_Path(ServletActionContext.getServletContext().getRealPath("/WEB-INF/classes/reports/"));
		this.voList.add(vo);
		if(StringUtils.isNotBlank(this.suc)){
			return this.suc;
		}
		return SUCCESS;		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rocky.project.action.report.IReporter#rtf()
	 */
	public String rtf() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public BaseService getBaseService() {
		return baseService;
	}

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	public Archive getArchive() {
		return archive;
	}

	public void setArchive(Archive archive) {
		this.archive = archive;
	}

	public ArchiveVO getVo() {
		return vo;
	}

	public void setVo(ArchiveVO vo) {
		this.vo = vo;
	}

	public List<ArchiveVO> getVoList() {
		return voList;
	}

	public void setVoList(List<ArchiveVO> voList) {
		this.voList = voList;
	}

}
